package huriezy.quickcontacts.activities.adapters;

import huriezy.quickcontacts.activities.fragments.ContactFragment;
import huriezy.quickcontacts.activities.fragments.EmailFragment;
import huriezy.quickcontacts.activities.fragments.LocationFragment;
import huriezy.quickcontacts.activities.fragments.NameAndPhoneFragment;
import huriezy.quickcontacts.activities.fragments.PictureFragment;
import huriezy.quickcontacts.activities.fragments.SocialNetworkFragment;
import huriezy.quickcontacts.activities.interfaces.IContactFragment;
import huriezy.quickcontacts.models.LogDebug;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;

public class NewContactFragmentPagerAdapter extends FragmentPagerAdapter {

	private final SparseArray<IContactFragment>	createdFragments	= new SparseArray<IContactFragment>();

	// private final Map<Class<? extends IContactFragment>, Boolean>
	// fragmentsIndex = new HashMap<Class<? extends IContactFragment>,
	// Boolean>();

	public NewContactFragmentPagerAdapter(FragmentManager fm) {
		super(fm);
		// initFragmentIndex();
	}

	// private void initFragmentIndex() {
	// fragmentsIndex.put(NameAndPhoneFragment.class, true);
	// fragmentsIndex.put(SocialNetworkFragment.class, true);
	// fragmentsIndex.put(PictureFragment.class, true);
	// fragmentsIndex.put(EmailFragment.class, true);
	// fragmentsIndex.put(LocationFragment.class, true);
	// }

	@Override
	public Fragment getItem(int index) {
		ContactFragment result = null;
		// switch (index) {
		// case 0:
		// if (fragmentsIndex.get(NameAndPhoneFragment.class)) {
		// result = NameAndPhoneFragment.newInstance("", "");
		// }
		// break;
		// case 1:
		// if (fragmentsIndex.get(SocialNetworkFragment.class)) {
		// result = SocialNetworkFragment.newInstance();
		// }
		// break;
		// case 2:
		// if (fragmentsIndex.get(PictureFragment.class)){
		// result = PictureFragment.newInstance("");
		// }
		// break;
		// case 3:
		// if (fragmentsIndex.get(EmailFragment.class)){
		// result = EmailFragment.newInstance("");
		// }
		// break;
		// case 4:
		// if (fragmentsIndex.get(LocationFragment.class)){
		// result = LocationFragment.newInstance("");
		// }
		// break;
		// default:
		// break;
		// }

		switch (index) {
			case 0:
				result = NameAndPhoneFragment.newInstance("", "");
				break;
			case 1:
				result = SocialNetworkFragment.newInstance();
				break;
			case 2:
				result = PictureFragment.newInstance("");
				break;
			case 3:
				result = EmailFragment.newInstance("");
				break;
			case 4:
				result = LocationFragment.newInstance("");
				break;
			default:
				break;
		}

		if (result != null) {
			if (createdFragments.get(index) == null) {
				createdFragments.put(index, result);
			}
		}

		return result;
	}

	@Override
	public int getCount() {
		return 5;
	}

	public void saveContactData() {
		for (int index = 0; index < createdFragments.size(); index++) {
			IContactFragment fragment = createdFragments.get(index);
			LogDebug.info("Save data for fragment " + fragment.getClass().getSimpleName(), fragment.getClass());
			fragment.saveContactData();
		}
	}

	public void clearContactData() {
		for (int index = 0; index < createdFragments.size(); index++) {
			IContactFragment fragment = createdFragments.get(index);
			fragment.clearContactData();
		}
	}

	public void updateData(int position) {
		IContactFragment fragment = createdFragments.get(position);
		if (fragment != null) {
			fragment.updateData();
		}
	}

//	public boolean hasPreferencesChanged(Map<Class<? extends IContactFragment>, Boolean> newFragmentPreferences) {
//		if (newFragmentPreferences == null || newFragmentPreferences.size() != fragmentsIndex.size()) {
//			return true;
//		}
//		else {
//			for (Class<? extends IContactFragment> fragmentClass : fragmentsIndex.keySet()) {
//				Boolean existingValue = fragmentsIndex.get(fragmentClass);
//				Boolean newValue = fragmentsIndex.get(fragmentClass);
//				if (existingValue == null || newValue == null) {
//					return true;
//				}
//				if (!existingValue.equals(newValue)) {
//					return true;
//				}
//			}
//		}
//		return false;
//	}
//
//	public void setFragmentPreferences(Map<Class<? extends IContactFragment>, Boolean> fragMap) {
//		if (fragMap != null) {
//			fragmentsIndex.clear();
//			fragmentsIndex.putAll(fragMap);
//			saveContactData();
//			createdFragments.clear();
//		}
//	}
//
//	public int getAvailableSize() {
//		int result = 0;
//		for (Class<?> fragmClass : fragmentsIndex.keySet()) {
//			Boolean value = fragmentsIndex.get(fragmClass);
//			if (value != null && value.booleanValue()) {
//				result++;
//			}
//		}
//		return result;
//	}
}
